﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Framework.Common
{
	public class CommonGlobal
	{
		private CommonGlobal()
		{ }
		private static readonly CommonGlobal instance = new CommonGlobal();
		public static CommonGlobal getInstance()
		{
			return instance;
		}

		#region 交互调用返回JSON格式

		/// <summary>
		/// 返回信息
		/// </summary>
		/// <param name="code">交互编码</param>
		/// <param name="Msg">响应消息</param>
		/// <returns></returns>
		public HttpResponseMessage ReturnJson(int code, string Msg)
		{

			string strResult = string.Format("{{\"Code\":\"{0}\",\"Msg\":\"{1}\",\"Data\":\"\"}}", code, Msg);
			return new HttpResponseMessage { Content = new StringContent(strResult, System.Text.Encoding.UTF8, "application/json") };
		}

		/// <summary>
		/// 返回信息
		/// </summary>
		/// <param name="code">交互编码</param>
		/// <param name="Msg">响应消息</param>
		/// <param name="Data">范围信息</param>
		/// <returns></returns>
		public HttpResponseMessage ReturnJson(int code, string Msg, string Data)
		{

			string strResult = string.Format("{{\"Code\":\"{0}\",\"Msg\":\"{1}\",\"Data\":{2}}}", code, Msg, Data);
			return new HttpResponseMessage { Content = new StringContent(strResult, System.Text.Encoding.UTF8, "application/json") };
		}

		/// <summary>
		/// 返回
		/// </summary>
		/// <param name="code"></param>
		/// <param name="Msg"></param>
		/// <param name="Data"></param>
		/// <returns></returns>
		public string ReturnJsonToString(int code, string Msg, string Data)
		{

			string strResult = string.Format("{{\"Code\":\"{0}\",\"Msg\":\"{1}\",\"Data\":{2}}}", code, Msg, Data);
			return strResult;
		}

		/// <summary>
		/// 返回
		/// </summary>
		/// <param name="code"></param>
		/// <param name="Msg"></param>
		/// <param name="Data"></param>
		/// <returns></returns>
		public string ReturnJsonToString(int code, string Msg)
		{

			string strResult = string.Format("{{\"Code\":\"{0}\",\"Msg\":\"{1}\",\"Data\":\"\"}}", code, Msg);
			return strResult;
		}

		/// <summary>
		/// 返回信息
		/// </summary>
		/// <param name="code">交互编码</param>
		/// <param name="Msg">响应消息</param>
		/// <param name="Data">返回集合</param>
		/// <returns></returns>
		public HttpResponseMessage ReturnJson(int code, string Msg, object Data)
		{
			string strData = JsonConvert.SerializeObject(Data).Replace("\"[", "[").Replace("]\"", "]").Replace("\"{", "{").Replace("}\"", "}");
			string strResult = string.Format("{{\"Code\":\"{0}\",\"Msg\":\"{1}\",\"Data\":{2}}}", code, Msg, strData);
			return new HttpResponseMessage { Content = new StringContent(strResult, System.Text.Encoding.UTF8, "application/json") };
		}

		/// <summary>
		/// 返回json数据例如分页
		/// SF 20140910
		/// </summary>
		/// <param name="code">交互编码</param>
		/// <param name="Msg">交互信息</param>
		/// <param name="PageIndex">当前页【仅在分页传其他传0】</param>
		/// <param name="PageSize">每页显示条数【仅在分页传其他传0】</param>
		/// <param name="TotleNum">总条数【仅在分页传其他传0】</param>
		/// <param name="dateFormat">日期格式【仅在分页传其他传空】</param>
		/// <param name="objectPage">返回分页对象【可以是datatable、List、Model】</param>
		/// <param name="obj">其他扩展数据</param>
		/// <returns></returns>
		public HttpResponseMessage ReturnPageJson(int code, string Msg, int PageIndex, int PageSize, int TotleNum, object objectPage, string dateFormat = "yyyy-MM-dd HH:mm", object obj = null)
		{
			if (objectPage == null)
			{
				objectPage = new string[] { };
			}
			string r_data = "";
			if (PageIndex == 0)
			{
				if (dateFormat != "")
				{
					IsoDateTimeConverter date = new IsoDateTimeConverter
					{
						DateTimeFormat = dateFormat
					};
					r_data = JsonConvert.SerializeObject(objectPage, date);
				}
				else
				{
					r_data = JsonConvert.SerializeObject(objectPage);
				}
			}
			else//分页序列化数据
			{

				Dictionary<string, object> dic_result = new Dictionary<string, object>();
				dic_result.Add("page", PageIndex);
				dic_result.Add("pageSize", PageSize);
				dic_result.Add("totalCount", TotleNum);
				dic_result.Add("list", objectPage);

				if (obj != null)
					dic_result.Add("obj", obj);

				if (dateFormat != "")
				{
					IsoDateTimeConverter date = new IsoDateTimeConverter
					{
						DateTimeFormat = dateFormat
					};
					r_data = JsonConvert.SerializeObject(dic_result, date);
				}
				else
				{
					r_data = JsonConvert.SerializeObject(dic_result);
				}
			}
			string strResult = string.Format("{{\"Code\":\"{0}\",\"Msg\":\"{1}\",\"Data\":{2}}}", code, Msg, r_data);
			return new HttpResponseMessage { Content = new StringContent(strResult, System.Text.Encoding.UTF8, "application/json") };
		}
		#endregion

		#region 替换html特殊字符

		/// <summary>
		/// 转义html脚本特殊字符
		/// </summary>
		/// <param name="parStr">参数</param>
		/// <returns></returns>
		public string HtmlScriptEncode(string parStr)
		{
			if (string.IsNullOrWhiteSpace(parStr))
			{
				return parStr;
			}
			else
			{
				parStr = parStr.Replace("<", "&lt;").Replace(">", "&gt;");

				return parStr;

			}

		}


		/// <summary>
		/// 转义特殊字符
		/// </summary>
		/// <param name="parStr">参数</param>
		/// <returns></returns>
		public string HtmlEncode(string parStr)
		{
			if (string.IsNullOrWhiteSpace(parStr))
			{
				return parStr;
			}
			else
			{

				parStr = System.Web.HttpUtility.HtmlEncode(parStr);

				return parStr;

			}

		}
		/// <summary>
		/// 过滤特殊字符
		/// 如果字符串为空，直接返回。
		/// ZCJ 2015-02-12
		/// </summary>
		/// <param name="str">需要过滤的字符串</param>
		/// <returns>过滤好的字符串</returns>
		public string ReplaceChar(string str)
		{
			if (string.IsNullOrEmpty(str))
			{
				return str;
			}
			else
			{
				str = str.Replace("\n", "");//换行
				str = str.Replace("\\n", "");
				str = str.Replace("\t", "");//制表符
				str = str.Replace("\\t", "");
				str = str.Replace("\r", "");//回车
				str = str.Replace("\\r", "");
				str = str.Replace("\b", "");//退格
				str = str.Replace("\\b", "");
				str = str.Replace("\f", "");//换页
				str = str.Replace("\\f", "");
				str = str.Replace("\v", "");
				str = str.Replace("\\v", "");
				str = str.Replace("\0", "");//空符号
				str = str.Replace("\\0", "");
				str = str.Replace("&quot;", "\"");
				return str;
			}
		}


		/// <summary>
		/// 字符串html格式转换
		/// 钟卫松 2014.09.19
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public string HtmlDecode(string sourceStr)
		{
			return System.Web.HttpUtility.HtmlDecode(sourceStr);
		}

		#endregion
	}
}
